package icu.sunway.gushenBlogBackEnd.Controller;

import icu.sunway.gushenBlogBackEnd.Entity.View.ResultVO;
import icu.sunway.gushenBlogBackEnd.Entity.View.User.*;
import icu.sunway.gushenBlogBackEnd.Service.MailService;
import icu.sunway.gushenBlogBackEnd.Service.UserService;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping("/api/user")
public class UserController {

    final UserService userService;
    final MailService mailService;

    public UserController(UserService userService, MailService mailService) {
        this.userService = userService;
        this.mailService = mailService;
    }

    @PostMapping(value = "/login")
    public ResultVO<String> login(@RequestBody UserLoginVO userLoginVO) {
        return userService.login(userLoginVO);
    }

    @PutMapping("/update")
    public ResultVO<String> updateUserInfo(@RequestHeader String token,
                                           @RequestBody UserUpdateInfoVO userUpdateInfoVO) {
        return userService.updateUserInfo(token, userUpdateInfoVO);
    }

    @GetMapping("/info")
    public ResultVO<UserVO> getUserInfo(@RequestHeader String token) {
        return userService.getUserInfo(token);
    }

    @GetMapping("/send_code")
    public ResultVO<Boolean> sendCode(@RequestParam String email) {
        return mailService.sendVerifyCode(email);
    }

    @GetMapping("/send_code_by_token")
    public ResultVO<Boolean> sendCodeByToken(@RequestHeader String token) {
        return userService.sendVerifyCodeByToken(token);
    }

    @PostMapping("/register")
    public ResultVO<String> register(@RequestBody UserRegisterVO userRegisterVO) {
        return userService.register(userRegisterVO);
    }

    @PostMapping("/change_password")
    public ResultVO<Boolean> changePassword(@RequestBody UserChangePasswordVO userChangePasswordVO,
                                            @RequestHeader String token) {
        return userService.changePassword(userChangePasswordVO, token);
    }

    @PutMapping("/reset_password")
    public ResultVO<Boolean> resetPassword(@RequestBody UserForgetPasswordVO userForgetPasswordVO) {
        return userService.resetPassword(userForgetPasswordVO);
    }

    @GetMapping("/check_admin")
    public ResultVO<Boolean> checkAdmin(@RequestHeader String token) {
        return userService.checkAdmin(token);
    }

    @PutMapping("/update_avatar")
    public ResultVO<Boolean> updateUserAvatar(@RequestParam String avatar,
                                              @RequestHeader String token) {
        return userService.updateUserAvatar(avatar, token);
    }
}
